<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//定义商品类
class GoodsController extends CommonController{
	private $model;
	private $GoodsdetailModel;
	private $GoodsattrModel;
	//定义构造函数
	public function __init(){
		parent::__init();
		$this->model=new \Common\Model\Goods;
		$this->GoodsdetailModel=new \Common\Model\Goodsdetail;
		$this->GoodsattrModel=new \Common\Model\Goodsattr;
	}
	//定义首页展示方法
	public function index(){
		$goods=$this->model->get();
		
//		p($goods);
		View::with('goods',$goods);
		View::make();
	}
	
	//添加商品方法
	public function add(){
		
		//判断用户是否有提交
		if(IS_POST){
			$_POST['dimg']=implode('|', $_POST['dimg']);
			
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功','index');
			
		}
		//获得分类
		$cate=new \Common\Model\Cate;
		$cate=Data::tree($cate->get(),'cname');
//		p($cate);
		View::with('cate',$cate);
		//获得品牌
		$brand=new \Common\Model\Brand;
		$brand=$brand->get();
		View::with('brand',$brand);
//		p($brand);
		View::make();
	}
	//删除
	public function del(){
		//获得gid
		$gid=Q("get.gid",0,"intavl");
		$this->model->where("gid=$gid")->delete();
		$this->GoodsdetailModel->where("goods_gid=$gid")->delete();
		$this->GoodsattrModel->where("goods_gid=$gid")->delete();
		View::success('删除成功');
	}
	
	public function edit(){		
		if(IS_POST){
//			p($_POST);die;
			$_POST['dimg']=implode('|', $_POST['dimg']);
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('添加成功');
		}
		//获得用户提交的数据
		$gid=Q('get.gid',0,'intval');
		//通过gid查询数据库的内容
		$goodsData=$this->model->where("gid={$gid}")->get();
		$detailData=$this->GoodsdetailModel->where("goods_gid={$gid}")->get();
		$attrData=$this->GoodsattrModel->where("goods_gid={$gid}")->get();
//		p($detaildata);
		$dimg=explode("|", $detailData[0]['dimg']);
		View::with('dimg',$dimg);
//		p($goodsData);
//		p($detailData);
//		p($attrData);
		View::with('goodsData',$goodsData);
		View::with('detailData',$detailData);
		View::with('attrData',$attrData);
		//获得分类
		$cate=new \Common\Model\Cate;
		$cate=Data::tree($cate->get(),'cname');
//		p($cate);
		View::with('cate',$cate);
		//获得品牌
		$brand=new \Common\Model\Brand;
		$brand=$brand->get();
//		p($brand);
		View::with('brand',$brand);
		$typeAttr=
		//商品属性表和类型属性表关联
		$attrData=$this->GoodsattrModel->join('t_attr','t_attr_taid','=','taid')->where("goods_gid={$gid}")->get();
		
		foreach($attrData as $k=>$v){
			$attrData[$k]['attvalue']=explode('|', $v['attvalue']);
		}
//		p($attrData);
		View::with('attrData',$attrData);
		View::make();
	}
	
	
	
	
	
	public function getTypeAttr(){
		$t_attr=new \Common\Model\Attribute;
		$tid=Q('post.id',0,'intval');
		$data=$t_attr->where("type_tid={$tid}")->get();
		foreach ($data as $k=>$v){
			$data[$k]['attvalue']=explode('|', $v['attvalue']);
		}
		echo json_encode($data);exit;
	}
	
	



	
	
	
	
	
	 //uploadify异步上传
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
}



 ?>